package es.udc.emarketapp.emarket.model.seller.dao;

import es.udc.emarketapp.emarket.model.client.vo.ClientVO;
import es.udc.emarketapp.emarket.model.seller.vo.SellerVO;
import es.udc.emarketapp.emarket.model.user.dao.AbstractSQLUserDAO;
import es.udc.emarketapp.emarket.model.user.vo.UserVO;
import es.udc.fbellas.j2ee.util.exceptions.InstanceNotFoundException;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.sql.GeneralOperations;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;


public abstract class AbstractSQLSellerDAO extends AbstractSQLUserDAO
        implements SQLSellerDAO{

    public boolean exists(Connection connection, String idVendedor)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT id_usuario FROM usuario "
                    + "WHERE id_usuario = ?";
            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            preparedStatement.setString(1, idVendedor);

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            return resultSet.next();

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }
    }//exists

    public boolean exists(Connection connection, String idComprador, String pass)
            throws InternalErrorException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean existsNifCif(Connection connection, String nifCif)
            throws InternalErrorException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {

            // Create "preparedStatement".
            String queryString = "SELECT nif_cif FROM usuario "
                    + "WHERE nif_cif = ?";
            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            preparedStatement.setString(1, nifCif);

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            return resultSet.next();

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }
    }

    public SellerVO findById(Connection connection, String idVendedor)
            throws InternalErrorException, InstanceNotFoundException {

        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            
            // Create "preparedStatement".
            String queryString = "SELECT nom_pila, ap1, ap2, nif_cif, dir, "
                    + "ciudad, cp, prov, pais, email, tlf1, tlf2, tipo_usuario, "
                    + "aprobado, valido FROM usuario WHERE id_usuario = ? ";
            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            preparedStatement.setString(1, idVendedor);

            // Execute query.
            resultSet = preparedStatement.executeQuery();

            if (!resultSet.next()) {
                throw new InstanceNotFoundException(idVendedor,
                        UserVO.class.getName());
            }

            // Get results.
            int i = 1;
            String nomPila = resultSet.getString(i++);
            String ap1 = resultSet.getString(i++);
            String ap2 = resultSet.getString(i++);
            String nifCif = resultSet.getString(i++);
            String dir = resultSet.getString(i++);
            String ciudad = resultSet.getString(i++);
            String cp = resultSet.getString(i++);
            String prov = resultSet.getString(i++);
            String pais = resultSet.getString(i++);
            String email = resultSet.getString(i++);
            String tlf1 = resultSet.getString(i++);
            String tlf2 = resultSet.getString(i++);
            String tipoUsuario = resultSet.getString(i++);
            boolean aprobado = resultSet.getBoolean(i++);
            boolean valido = resultSet.getBoolean(i++);


             // Create "preparedStatement".
            queryString = "SELECT nom_emp, "
                    + "n_reg_merc, fax, iban, swift FROM vendedor "
                    + "WHERE id_vendedor = ? ";
            preparedStatement = connection.prepareStatement(queryString);

            // Fill "preparedStatement".
            preparedStatement.setString(1, idVendedor);

             // Execute query.
            resultSet = preparedStatement.executeQuery();

            if (!resultSet.next()) {
                throw new InstanceNotFoundException(idVendedor,
                        ClientVO.class.getName());
            }

            // Get results.
            i = 1;
            String nomEmp = resultSet.getString(i++);
            String nRegMerc = resultSet.getString(i++);
            String fax = resultSet.getString(i++);
            String iban = resultSet.getString(i++);
            String swift = resultSet.getString(i++);


            // Return the value object.
            return new SellerVO(idVendedor, null, nomPila, ap1,
                ap2, nifCif, dir, ciudad, cp, prov, pais, email, tlf1, tlf2,
                tipoUsuario,  aprobado, valido, nomEmp, nRegMerc, fax, iban,
                swift);

        } catch (SQLException e) {
            throw new InternalErrorException(e);
        } finally {
            GeneralOperations.closeResultSet(resultSet);
            GeneralOperations.closeStatement(preparedStatement);
        }
    }

    public SellerVO findById(Connection connection, String idVendedor, String pass)
            throws InternalErrorException, InstanceNotFoundException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<SellerVO> findAll(Connection connection, int startIndex, int count)
            throws InternalErrorException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<SellerVO> findByPais(Connection connection, String pais,
            int startIndex, int count) throws InternalErrorException {
        throw new UnsupportedOperationException("Not supported yet.");
    }


}
